package com.axend.lib_base.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.lang.ref.WeakReference;

/**
 * @author admins
 * @description
 */
public class SoftKeyBoardUtils {
    private WeakReference<View> rootViewRef;
    private int rootViewVisibleHeight;
    private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
    private final Rect r = new Rect();
    private ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener;

    public SoftKeyBoardUtils(Activity activity) {
        rootViewRef = new WeakReference<>(activity.getWindow().getDecorView());
        globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                rootViewRef.get().getWindowVisibleDisplayFrame(r);
                int visibleHeight = r.height();
                if (rootViewVisibleHeight == 0) {
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                if (rootViewVisibleHeight == visibleHeight) {
                    return;
                }

                if (rootViewVisibleHeight - visibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                if (visibleHeight - rootViewVisibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }
            }
        };
        rootViewRef.get().getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
    }

    public void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener listener) {
        this.onSoftKeyBoardChangeListener = listener;
    }

    public interface OnSoftKeyBoardChangeListener {
        void keyBoardShow(int height);
        void keyBoardHide(int height);
    }

    public void setListener(OnSoftKeyBoardChangeListener listener) {
        setOnSoftKeyBoardChangeListener(listener);
    }

    public static void hideKeyBoard(Context context, EditText view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public void dispose() {
        View view = rootViewRef.get();
        if (view != null && globalLayoutListener != null) {
            view.getViewTreeObserver().removeOnGlobalLayoutListener(globalLayoutListener);
        }
    }

}

